<?php
namespace App\Http\Controllers\AdminApi;

use App\Model\ClassTable;
use App\Repository\BaseController\ApiController;
use App\Repository\Common\ConstTable;
use App\Repository\Sys\JsonRespose;
use App\Repository\Sys\Str;
//分类管理
class ClassController extends ApiController{
    //分类-列表
    public function getClassList(){
    	$id_category = \Request::get('id_category');
    	$status = \Request::get('status',0);
        return JsonRespose::done(ClassTable::getClassList($id_category,$status));
    }
    //分类-添加修改
    public function postClassSave(){
        //获取所有参数到$param数组
        $param = \Request::all();
        $data = [
            'id' => $param['id'],
            'id_category' => $param['id_category'],
            'name' => $param['name'],
            'depth' => $param['depth'],
            'icon' => $param['icon'],
            'desc' => $param['desc'],
        ];
        //判断是否存在id
        if (isset($param['id']) and $param['id']) {
            //根据id更新数据
            $res = ClassTable::ORM()->where('id', $param['id'])->update($data);
        } else {
        	$data['status']=1;
            //否则插入数据
            $res = ClassTable::ORM()->insertGetId($data);
            //自增id更新为排序id
            ClassTable::ORM()->where('id', $res)->update(['sort' => $res]); //排序id
            $param['id'] = $res;
        }
        if ($res) {
            //接口返回数据
            return JsonRespose::success(['id' => $param['id']]);
        } else {
            return JsonRespose::fail();
        }
    }
    //分类-详情
    public function getClassInfo(){
        $id = \Request::get('id');
        return JsonRespose::done(ClassTable::getClassInfo($id));
    }
    //分类-状态修改
    public function postClassStatus(){
        $id = \Request::get('id');
        $status = \Request::get('status');
        //取得当前数据
        $res = ClassTable::ORM()->where('id', $id)->update(['status' => $status]);
        if ($res) {
            return JsonRespose::success();
        } else {
            return JsonRespose::fail();
        }
    }
    //分类-删除
    public function postClassDel(){
        $id = \Request::get('id');
        //取得当前数据
        $res = ClassTable::ORM()->where('id', $id)->update(['status' => -1]);
        if ($res) {
            return JsonRespose::success();
        } else {
            return JsonRespose::fail();
        }
    }
    //分类-修改排序
   	public function postClassSort(){
        $id = \Request::get('id');
        $sort = \Request::get('sort');
        //取得当前数据
        $res = ClassTable::ORM()->where('id', $id)->update(['sort' => $sort]);
        if ($res) {
            return JsonRespose::success();
        } else {
            return JsonRespose::fail();
        }
    }
}